package org.duanyu.shopping_manager_api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dubbo.config.annotation.DubboReference;
import org.duanyu.shopping_common.pojo.Specification;
import org.duanyu.shopping_common.pojo.SpecificationOptions;
import org.duanyu.shopping_common.result.BaseResult;
import org.duanyu.shopping_common.service.SpecificationService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/specification")
public class SpecificationController {
    @DubboReference
    private SpecificationService specificationService;

    /**
     * 添加商品规格
     * @param specification 商品规格对象
     * @return 返回结果
     */
    @PostMapping("/add")
    public BaseResult add(@RequestBody Specification specification) {
        specificationService.add(specification);
        return BaseResult.success();
    }

    /**
     * 更新商品规格
     *
     * @param specification 商品规格对象
     * @return 返回结果
     */
    @PutMapping("/update")
    public BaseResult update(@RequestBody Specification specification) {
        specificationService.update(specification);
        return BaseResult.success();
    }

    /**
     * 删除商品规格
     *
     * @param ids 商品规格id数组
     * @return 返回结果
     */
    @DeleteMapping("/delete")
    public BaseResult delete(Long[] ids) {
        specificationService.delete(ids);
        return BaseResult.success();
    }

    /**
     * 根据id查询商品规格
     *
     * @param id 商品规格id
     * @return 返回结果
     */
    @GetMapping("/findById")
    public BaseResult<Specification> findById(Long id) {
        return BaseResult.success(specificationService.findById(id));
    }

    /**
     * 分页查询商品规格
     * @param page 页码
     * @param size 每页条数
     * @return 返回结果
     */
    @GetMapping("/search")
    public BaseResult<Page<Specification>> search(int page, int size) {
        return BaseResult.success(specificationService.search(page, size));
    }

    /**
     * 根据商品类型id查询商品规格
     * @param id 商品类型id
     * @return 返回结果
     */
    @GetMapping("/findByProductTypeId")
    public BaseResult<List<Specification>> findByProductTypeId(Long id) {
        return BaseResult.success(specificationService.findByProductTypeId(id));
    }

    /**
     *  新增商品规格规格项
     * @param specificationOptions 商品规格项集合
     * @return 返回结果
     */
    @PostMapping("/addOption")
    public BaseResult addOption(@RequestBody SpecificationOptions specificationOptions) {
        specificationService.addOption(specificationOptions);
        return BaseResult.success();
    }

    /**
     * 删除商品规格项
     * @param ids 商品规格项id集合
     * @return 返回结果
     */
    @DeleteMapping("/deleteOption")
    public BaseResult deleteOption(Long[] ids){
        specificationService.deleteOption(ids);
        return BaseResult.success();
    }

}
